<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 9/23/14
 * Time: 9:15 PM
 */

namespace Backend\Modules\Upload\Controllers;
use Common\Models\Images\ImageLink;
use Common\Models\Images\Images;
use Library\Upload\UploadHandler;

class UploadController extends \BackendController
{
    public function indexAction()
    {
        if($this->request->isPost())
        {
            //die(print_r($this->request->getUploadedFiles()));
            $uploadHandler = new UploadHandler($option = array('param_name' => 'file'));
            die(print_r($uploadHandler));
        }

        if ($this->request->isAjax())
        {
            $uploadHandler = new UploadHandler($option = array('param_name' => 'file'));
            die(print_r($uploadHandler));

            //$uploadHandler->post();
            //die(print_r($this->request->getPost()));
            //$image = $this->request->getPost('file');
            /*foreach ($this->request->getUploadedFiles() as $file) {

                //Print file details
                echo $file->getName(), " ", $file->getSize(), "\n";

                //Move the file into the application
                $file->moveTo('files/' . $file->getName());
            }*/
            return json_encode($uploadHandler);
        }
    }

    public function doUploadAction()
    {

    }

    public function deteleImageLinkAction($id)
    {
        $imageLink = ImageLink::findFirstById((int)$id);
        if($imageLink)
        {
            if($imageLink->delete())
            {
                $image = Images::findFirstById($imageLink->getImageId());
                if($image)
                {
                    $image->delete();
                }
            }
        }
        $this->flashSession->success("Delete image success full!");
        $this->view->disable();
        return $this->response->redirect($_SERVER['HTTP_REFERER']);
    }
}